﻿using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DemoManageSys.Areas.Filters;
using Models.DTO;
using Microsoft.AspNetCore.Mvc;
using Models;
using IDAL;
using IBLL;
using Microsoft.AspNetCore.Http;

namespace DemoManageSys.Areas.API.Controllers
{
    [Area("API")]
    public class CustomerInfoController : Controller
    {
        private ICustomerInfoBLL _customerInfoBLL;

        public CustomerInfoController(
            ICustomerInfoBLL customerInfoBLL
            )
        {
            _customerInfoBLL = customerInfoBLL;
        }

        #region 登录方法
        /// <summary>
        /// 登录方法
        /// </summary>
        /// <param name="customerAccount">账号</param>
        /// <param name="customerPassword">密码</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult LoginIndex(string customerAccount, string customerPassword)
        {
            //序列化和反序列化
            //var jsonStr = JsonConvert.SerializeObject(result);
            //result = JsonConvert.DeserializeObject<Return>(jsonStr);
            ReturnResule result = new ReturnResule();

            if (string.IsNullOrWhiteSpace(customerAccount))
            {
                result.Msg = "账号不能为空";
                return Json(result);
            }
            if (string.IsNullOrWhiteSpace(customerPassword))
            {
                result.Msg = "密码不能为空";
                return Json(result);
            }

            string msg;
            string customerName;
            string customertId;
            result.IsSuccess = _customerInfoBLL.LoginIndex(customerAccount, customerPassword, out msg, out customerName, out customertId);
            if (result.IsSuccess)
            {
                result.Code = 200;
                result.Data = new { customertId, customerName };

                result.Msg = msg;
                //session
                HttpContext.Session.SetString("UserId", customertId);
                HttpContext.Session.SetString("UserName", customerName);
                //cookie
                CookieOptions options = new CookieOptions()
                {
                    Expires = DateTime.Now.AddHours(2)
                };
                Response.Cookies.Append("UserId", customertId);
                Response.Cookies.Append("UserName", customerName);

            }
            else
            {
                result.Msg = msg;
                result.Code = 500;
            }
            return Json(result);

        }
        #endregion

        #region 获取的方法 (GetCustomerInfo)
        [HttpGet]
        public IActionResult GetCustomerInfo(int page, int limit, string customerId)
        {
            int count;
            //调用BLL的查询方法
                 
            List<GetCustomerInfoDTO> getCustomerInfoDTOs = _customerInfoBLL.GetCustomerInfo(page, limit, customerId, out count);


            ReturnResule result = new ReturnResule()
            {
                Code = 0,
                Msg = "获取成功",
                Data = getCustomerInfoDTOs,
                Count = count
            };

            return Json(result);
        }
        #endregion ()

        #region 添加的方法 (CreateCustomerInfo)
        [HttpPost]
        public IActionResult CreateCustomerInfo([FromForm] CustomerInfo cust)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();
            string msg;//定义返回消息
            resule.IsSuccess = _customerInfoBLL.CreateCustomerInfo(cust, out msg);//调用方法传值
            if (resule.IsSuccess)
            {
                resule.Code = 200;
                resule.Msg = msg;
            }
            else
            {
                resule.Msg = msg;

            }
            return Json(resule);
        }
        #endregion

        #region 修改顾客的方法 (UpdateCustomerInfo)
        /// <summary>
        /// 修改顾客的方法
        /// </summary>
        /// <param name="cust"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateCustomerInfo([FromForm] CustomerInfo cust)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();

            string msg;//定义返回消息
            resule.IsSuccess = _customerInfoBLL.UpdateCustomerInfo(cust, out msg);//调用方法传值

            resule.Msg = msg;
            if (resule.IsSuccess)
            {
                resule.Code = 200;
            }

            return Json(resule);
        }
        #endregion

        #region 修改顾客头像的方法 (UpdateCustomerInfo)
        /// <summary>
        /// 修改顾客头像的方法
        /// </summary>
        /// <param name="custheader"></param>
        /// <param name="custId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateCustomerInfoHeader(string custheader,string custId)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();

            string msg;//定义返回消息
            resule.IsSuccess = _customerInfoBLL.UpdateCustomerInfoHeader(custheader, custId, out msg);//调用方法传值

            resule.Msg = msg;
            if (resule.IsSuccess)
            {
                resule.Code = 200;
            }

            return Json(resule);
        }
        #endregion
    }
}